using Cinemachine;
using Combat;
using UnityEngine;

namespace Visual
{
    public class ScreenShake : MonoBehaviour
    {
        private CinemachineImpulseSource _source;


        private void Awake()
        {
            _source = GetComponent<CinemachineImpulseSource>();
            ExplodeOnDied.AnyThingExploded += ExplodeOnDied_AnyThingExploded;
            Rocket.AnyRocketExploded += Rocket_AnyRocketExploded;
        }

        private void OnDestroy()
        {
            ExplodeOnDied.AnyThingExploded -= ExplodeOnDied_AnyThingExploded;
            Rocket.AnyRocketExploded -= Rocket_AnyRocketExploded;
        }

        private void ExplodeOnDied_AnyThingExploded()
        {
            Shake();
        }
        
        private void Rocket_AnyRocketExploded()
        {
            Shake(2f);
        }

        private void Shake(float intensity = 1f)
        {
            _source.GenerateImpulse(intensity);
        }
    }
}